# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from GlobalVar import globalVar
from Crawl import Crawl
from NodeStorer import NodeStorer
from DependStorer import DependStorer
from neo4j import GraphDatabase
import time
import logging

import argparse
MAX_COUNT = 54821


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    logging.basicConfig(filename='time.log', level=logging.INFO,
                        format='%(asctime)s %(levelname)s: %(message)s')
    parser = argparse.ArgumentParser()  # 创建一个解析对象
    parser.add_argument('--hasJson', default=True)
    parser.add_argument('--storeNode', default=False)
    parser.add_argument('--continuePre', default=False)
    parser.add_argument('--continueStoreNode', default=False)  # 这里好像还有些bug
    parser.add_argument('--continueStoreDepend', default=True)
    parser.add_argument('--preCount', default=0)
    parser.add_argument('-uri', default="neo4j://211.71.15.39:12687")
    # parser.add_argument('-uri', default="neo4j: // localhost: 7687")
    parser.add_argument('--user', default="neo4j")
    parser.add_argument('--password', default="sdpneo4j")
    parser.add_argument('--batch_size', default=2000)
    args = parser.parse_args()
    if not args.hasJson:  # 先把全版本软件包的JSON文件下载到本地
        # 取到前1000个包
        globalVar.get_starter_pkgs()
        # 开始遍历
        crawl = Crawl()
        crawl.get_closure(bool(args.continuePre), int(args.preCount))
        # print(globalVar.starter_pkgs)
    # globalVar.pkg_queue = globalVar.load_list(f'./pkg_queue_final.txt')
    # print(f'loaded ./pkg_queue_final.txt')

    globalVar.miss_pkg = globalVar.load_list(f'./miss_pkg_final.txt')
    print(f'loaded ./miss_pkg_final.txt')
    # globalVar.finished_pkg = globalVar.load_list(f'./finished_pkg_final.txt')
    # print(f'loaded ./finished_pkg_final.txt')
    # 开始存数据库
    dbDriver = driver = GraphDatabase.driver(args.uri,
                                             auth=(args.user, args.password),
                                             max_connection_lifetime=3600 * 24 * 30,
                                             keep_alive=True)

    # 先将所有节点都存入数据库，再存储依赖关系
    start_time = time.time()
    logging.info(f'start time: {start_time}')
    mid_time = 0
    if args.storeNode:
        if args.continueStoreNode:
            globalVar.finished_pkg_node = globalVar.load_list("./finished_pkg_node_tmp.txt")
            print(f'./finished_pkg_node_tmp.txt')
            logging.info(f'loaded finished_pkg_node len: {len(globalVar.finished_pkg_node)}')
        nodeStorer = NodeStorer(dbDriver, int(args.batch_size))
        nodeStorer.store_all(args.continueStoreNode)
        mid_time = time.time()
        logging.info(f'finish store nodes time: {mid_time}')
        logging.info(f'store allV nodes time: {mid_time - start_time}')
    else:
        print(f'skip store nodes')
        logging.info(f'=================skip store nodes=================')
        mid_time = time.time()
    if args.continueStoreDepend:
        globalVar.finished_pkg_depend = globalVar.load_list("./finished_pkg_depend_tmp.txt")
        print(f'./finished_pkg_depend_tmp.txt')

    dependStorer = DependStorer(dbDriver, int(args.batch_size))
    dependStorer.store_all(args.continueStoreDepend)
    end_time = time.time()
    logging.info(f'finish store dependency time: {mid_time}')
    logging.info(f"store all node's dependency time: {end_time - mid_time}")
    logging.info(f'store time: {end_time-start_time}')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
